{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 开启通义千问大模型"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d4e07c9036f7ad17"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "import langchain\n",
    "from langchain_community.llms import Tongyi"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "28a5c45893d99b98"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"] = \"sk-edf4bccb72d14fbabcb7f1d9369c8a65\""
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:37:13.915671500Z",
     "start_time": "2024-05-07T15:37:13.909555300Z"
    }
   },
   "id": "529e6962027617fb"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "'Hello! How can I help you today?'"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm = Tongyi()\n",
    "llm.predict(\"hello\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:41:31.006718900Z",
     "start_time": "2024-05-07T15:41:30.450216300Z"
    }
   },
   "id": "13f6c047657100cc"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 使用模板做起名大师"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e8e85680bddec2e9"
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是一个起名大师，请模仿示例起3个名字,国家是中国, 在中国里男孩经常被叫做狗剩,女孩经常被叫做翠花，请直接给出以逗号分隔的名字，不要其他内容\n"
     ]
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt = PromptTemplate.from_template(\"你是一个起名大师，请模仿示例起3个名字,国家是{country}, 在{country}里男孩经常被叫做{boy_name},女孩经常被叫做{girl_name}，请直接给出以逗号分隔的名字，不要其他内容\")\n",
    "message = prompt.format(country=\"中国\", boy_name=\"狗剩\", girl_name=\"翠花\")\n",
    "print(message)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:47:23.732158400Z",
     "start_time": "2024-05-07T15:47:23.725276600Z"
    }
   },
   "id": "e3d4c185b8998522"
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "'强生,丽华,小明,红梅,志刚,秀兰'"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "llm.predict(message)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:47:25.372230200Z",
     "start_time": "2024-05-07T15:47:24.461281900Z"
    }
   },
   "id": "af53560b25e9d9e6"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 格式化输出"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "df4afa1735274ce7"
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "from langchain.schema import BaseOutputParser, T\n",
    "\n",
    "\n",
    "#自定义输出\n",
    "class CustomOutputParser(BaseOutputParser):\n",
    "    def parse(self, text: str) -> T:\n",
    "        \"\"\"分割字符串为列表，以逗号分隔\"\"\"\n",
    "        return text.strip().split(\",\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:51:39.658529800Z",
     "start_time": "2024-05-07T15:51:39.653450500Z"
    }
   },
   "id": "fb9c26fd8bf86d3a"
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "['张三', '李四']"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 看下自定义的效果\n",
    "CustomOutputParser().parse(\"张三,李四\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:51:40.018850200Z",
     "start_time": "2024-05-07T15:51:40.015244800Z"
    }
   },
   "id": "914c0da0beb4a8fc"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 实现完整的起名大师"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "802fe67fca3959e7"
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "['狗蛋', '铁柱', '小芳', '菊花', '喜鹊', '腊梅']"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "prompt = PromptTemplate.from_template(\"你是一个起名大师，请模仿示例起3个名字,国家是{country}, 在{country}里男孩经常被叫做{boy_name},女孩经常被叫做{girl_name}，请直接给出以逗号分隔的名字，不要其他内容\")\n",
    "message = prompt.format(country=\"中国\", boy_name=\"狗剩\", girl_name=\"翠花\")\n",
    "llm_response = llm.predict(message)\n",
    "CustomOutputParser().parse(llm_response)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-07T15:52:52.465796900Z",
     "start_time": "2024-05-07T15:52:51.693498300Z"
    }
   },
   "id": "d5d4c2da77581a30"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d04f84a31d34f004"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
